package com.dony.web.controller.order;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dony.common.core.controller.BaseController;
import com.dony.common.core.domain.AjaxResult;
import com.dony.common.core.page.TableDataInfo;
import com.dony.common.utils.DateUtils;
import com.dony.order.domain.Order;
import com.dony.order.domain.PurchaseRecord;
import com.dony.order.domain.vo.OrderPackVo;
import com.dony.order.service.*;
import com.dony.shop.domain.Supplier;
import com.dony.shop.service.ISupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author sundw
 * @version 1.0.0
 * @description ProductController
 * @since 2023/2/23 18:42
 */
@Slf4j
@RestController
@RequestMapping("/shopee/order")
public class OrderController extends BaseController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderPackService orderPackService;
    @Autowired
    ILogisticsService logisticsService;



    /**
     * 修改订单状态
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(orderService.updateById(order));
    }

    /**
     * 批量更新方法
     */
    @PostMapping(value = "/update/status/{sysStatus}/{orderSns}")
    public AjaxResult updateStatus(@PathVariable int sysStatus,
                                   @PathVariable String[] orderSns) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("sys_status", sysStatus);
        updateWrapper.in("order_sn", orderSns);
        return toAjax(orderService.update(updateWrapper));
    }


    /**
     * 同步订单
     */
    @RequestMapping(value = "/sync")
    public AjaxResult sync() {
        return AjaxResult.success(orderService.syncOrder());
    }

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OrderPackVo order) {
        startPage();
        //查询订单
        List<Order> orders = orderPackService.listByParam(order);
        TableDataInfo dataTable = getDataTable(orders);
        //封装order数据
        List<OrderPackVo> orderVos = orderPackService.handleOrderList(orders);
        dataTable.setRows(orderVos);
        return dataTable;
    }

    /**
     * 查询订单列表
     */
    @GetMapping("/get/inCancel")
    public AjaxResult getInCancelOrder() {
        return AjaxResult.success(orderPackService.getInCancelOrder());
    }


    /**
     * 订单打印
     */
    @GetMapping("/print/{orderSns}")
    @ResponseBody
    public AjaxResult print(@PathVariable("orderSns") String[] orderSns) {
        return AjaxResult.success(orderPackService.printOrder(orderSns));

    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{orderSn}")
    public AjaxResult getInfo(@PathVariable("orderSn") String orderSn) {
        return AjaxResult.success(orderService.getById(orderSn));
    }


}
